import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:
 * Date: 2025-10-04
 * Time: 9:25
 */
public class demo1 {
    public static void main1(String[] args) throws InterruptedException {
        Object locker1 = new Object();
        Object locker2 = new Object();

        Thread t1 = new Thread(() -> {
            synchronized (locker1){
                System.out.println("t1拿到一个线程");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                synchronized (locker2){
                    System.out.println("t1拿到所有线程");
                }
            }
        });

        Thread t2 = new Thread(() -> {
            synchronized (locker2){
                System.out.println("t2拿到一个线程");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                synchronized (locker1){
                    System.out.println("t2拿到所有线程");
                }
            }
        });

        t1.start();
        t2.start();
        t1.join();
        t2.join();

    }

    public static int flag = 0;

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            while(flag == 0){

            }
            System.out.println("t1 线程结束");
        });

        Thread t2 = new Thread(() -> {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入flag的值：");
            flag = sc.nextInt();
        });

        t1.start();
        t2.start();
    }
}
